package com.jianhua.backend.controller;

import com.jianhua.backend.common.R;
import com.jianhua.backend.domain.entity.UserAddresses;
import com.jianhua.backend.service.UserAddressesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 存储用户在各场馆的配送地址信息 前端控制器
 * </p>
 *
 * @author mahang
 * @since 2025-07-02
 */
@RestController
@RequestMapping("/addresses")
public class UserAddressesController {

    @Autowired
    private UserAddressesService userAddressesService;

    // 获取用户的地址
    @GetMapping("/{userId}")
    public List<UserAddresses> getUserAddresses(@PathVariable Integer userId) {
        return userAddressesService.getUserAddressesByUserId(userId);
    }

    // 新增地址
    @PostMapping("/add")
    public boolean addUserAddress(@RequestBody UserAddresses address) {
        return userAddressesService.addAddress(address);
    }

    /**
     * 更新地址信息
     * @param addressId 地址ID
     * @param address 更新后的地址信息
     * @return 操作结果
     */
    @PutMapping("/{addressId}")
    public R updateAddress(@PathVariable Long addressId, @RequestBody UserAddresses address) {
        userAddressesService.updateAddress(address);
        return R.success(null);
    }

    /**
     * 删除地址
     * @param addressId 地址ID
     * @return 操作结果
     */
    @DeleteMapping("/{addressId}")
    public R deleteAddress(@PathVariable Integer addressId) {
        userAddressesService.deleteAddress(addressId);
        return R.success(null);
    }

}
